package cn.jsj.dispose;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 增加付款详情处理类
 * @author LI
 *
 */
 public class PayDetailInfoRequest extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 调试信息
		 cn.jsj.utils.SysLogger.PrintInfo("增加付款情况");

		 //新建付款详情类
		 cn.jsj.pojo.TblPayDetailInfo tpdi=new cn.jsj.pojo.TblPayDetailInfo();
		 //获取合同总金额
		 int contractMoney=Integer.parseInt(request.getParameter("pContractMoney").trim());
		 tpdi.setCaseNumber(request.getParameter("payLawyercontractId").trim());
		 tpdi.setPaymentMoney(Integer.parseInt(request.getParameter("payMoney").trim()));
		 tpdi.setPaymentRemark(request.getParameter("payRemark").trim());
		 tpdi.setPaymentTime(cn.jsj.utils.TimeDispose.transferDate(request.getParameter("payTime").trim()));

		 //赋值完成
		 cn.jsj.utils.SysLogger.PrintInfo("赋值完成！");

		//输出流
		 java.io.PrintWriter out=response.getWriter();

		 //确认付款总额不多于合同金额
		 cn.jsj.dao.TblPayDetailDAO tpdd=new cn.jsj.dao.TblPayDetailDAO();
		 int payMoney=tpdd.getTotalPayMoney(request.getParameter("payLawyercontractId").trim());
		 //如果付款总金额大于合同金额
		 int totalPayMoney=Integer.parseInt(request.getParameter("payMoney").trim())+payMoney;
		 if(totalPayMoney>contractMoney){
			 out.println("{message:'付款失败,请确认付款金额是否正确'}");
			 out.flush();
			 out.close();
			 return;
		 }
		 //保存
		 int saveResult=tpdd.savePayDetialInfo(tpdi);

		 //调试信息
		 cn.jsj.utils.SysLogger.PrintInfo("保存结果------>"+saveResult);


		 //如果保存成功
		 if(saveResult==1){
			 out.println("{message:'付款成功！'}");
		 }else{

			 out.println("{message:'付款失败！'}");
		 }
		 out.flush();
		 out.close();

	 }
}